package com.recource.controller;

import com.recource.dao.FileDao;
import com.recource.domain.FileAssets;
import com.recource.util.SystemUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author yujie
 * @createTime 2022/11/26 11:21
 * @description 文件上传接口
 */
@RestController
public class FileController {

    @Resource
    private FileDao fileDao;

    @GetMapping("/previewFile")
    public void previewFile(String code, HttpServletResponse response){
        LambdaQueryWrapper<FileAssets> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FileAssets::getId, code);
        FileAssets fileAssets = fileDao.selectOne(wrapper);
        response.setContentType(fileAssets.getFileType());
        FileInputStream file = SystemUtil.getFile(fileAssets.getFlod(), fileAssets.getFileKey());
        try {
            file.transferTo(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
